clap-stdin
This library offers two wrapper types for clap
Arg
s that help
for cases where values may be passed in via stdin
. When an Arg
value is to be read
from stdin
, the user will pass the commonly used stdin
alias: -
MaybeStdin
: Used when a value can be passed in via args ORstdin
FileOrStdin
: Used when a value can be read in from a file ORstdin
MaybeStdin
Example usage with clap
's derive
feature for a positional argument:
use Parser;
use MaybeStdin;
let args = parse;
println!;
Calling this CLI:
# using stdin for positional arg value
|
value=testing
Compatible Types
[MaybeStdin
] can wrap any type that matches the trait bounds for Arg
: FromStr
and Clone
use PathBuf;
use Parser;
use MaybeStdin;
|
FileOrStdin
Example usage with clap
's derive
feature for a positional argument:
use Parser;
use FileOrStdin;
#
Calling this CLI:
# using stdin for positional arg value
|
input=testing
# using filename for positional arg value
input=testing
Compatible Types
[FileOrStdin
] can wrap any type that matches the trait bounds for Arg
: FromStr
and Clone
use PathBuf;
use Parser;
use FileOrStdin;
# Value from stdin
|
# Value from file
Reading from Stdin without special characters
When using [MaybeStdin
] or [FileOrStdin
], you can allow your users to omit the "-" character to read from stdin
by providing a default_value
to clap. This works with positional and optional args:
use Parser;
use FileOrStdin;
#
Calling this CLI:
# using stdin for positional arg value
|
input=testing
# using filename for positional arg value
input=testing
Async Support
FileOrStdin
can also be used with tokio::io::AsyncRead
using the tokio
feature. See [FileOrStdin::contents_async
] and [FileOrStdin::into_async_reader
] for examples.
Using MaybeStdin
or FileOrStdin
multiple times
Both [MaybeStdin
] and [FileOrStdin
] will check at runtime if stdin
is being read from multiple times. You can use this
as a feature if you have mutually exclusive args that should both be able to read from stdin, but know
that the user will receive an error if 2+ MaybeStdin
args receive the "-" value.
For example, this compiles:
use ;
and it will work fine if the stdin alias -
is only passed for one of the arguments:
|
But if stdin
is attempted to be used for both arguments, there will be no value for the second
arg
|
License
clap-stdin
is both MIT and Apache License, Version 2.0 licensed, as found
in the LICENSE-MIT and LICENSE-APACHE files.